/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zhpan.bannerview.drawer;

import ohos.agp.render.Paint;

import com.zhpan.bannerview.base.MeasureResult;
import com.zhpan.bannerview.options.IndicatorOptions;
import com.zhpan.bannerview.options.Options;

/**
 * Paint.Style.FILL设置只绘制图形内容
 * Paint.Style.STROKE设置只绘制图形的边
 * Paint.Style.FILL_AND_STROKE设置都绘制
 *
 * @since 2021-03-24
 */
public abstract class BaseDrawer implements IDrawer {
    /**
     * INDICATOR_PADDING_ADDITION
     */
    protected final int INDICATOR_PADDING_ADDITION = 6;
    /**
     * INDICATOR_PADDING
     */
    protected final int INDICATOR_PADDING = 3;
    /**
     * maxWidth
     */
    protected float maxWidth;
    /**
     * minWidth
     */
    protected float minWidth;
    /**
     * mPaint
     */
    protected Paint mPaint = new Paint();
    /**
     * mOptions
     */
    protected IndicatorOptions mOptions;
    private MeasureResult mMeasureResult;

    /**
     * BaseDrawer
     *
     * @param options 參數
     */
    public BaseDrawer(IndicatorOptions options) {
        this.mOptions = options;
        mMeasureResult = new MeasureResult();
    }

    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
    }

    @Override
    public MeasureResult onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        maxWidth = coerceAtLeast(mOptions.getNormalSliderWidth(), mOptions.getCheckedSliderWidth());
        minWidth = coerceAtMost(mOptions.getNormalSliderWidth(), mOptions.getCheckedSliderWidth());

        int orientation = mOptions.getOrientation();
        if (orientation == Options.Orientation.INDICATOR_VERTICAL) {
            mMeasureResult.setMeasureResult(measureHeight(), measureWidth());
        } else {
            mMeasureResult.setMeasureResult(measureWidth(), measureHeight());
        }
        return mMeasureResult;
    }

    /**
     * measureHeight
     *
     * @return int
     */
    protected int measureHeight() {
        return (int) (mOptions.getSliderHeight() + INDICATOR_PADDING);
    }

    /**
     * measureWidth
     *
     * @return int
     */
    protected int measureWidth() {
        int pageSize = mOptions.getPageSize();
        float sliderGap = mOptions.getSliderGap();
        return (int) (((pageSize - 1) * sliderGap + maxWidth + (pageSize - 1) * minWidth) + INDICATOR_PADDING_ADDITION);
    }

    /**
     * isWidthEquals
     *
     * @return boolean
     */
    protected boolean isWidthEquals() {
        return mOptions.getNormalSliderWidth() == mOptions.getCheckedSliderWidth();
    }

    /**
     * coerceAtLeast
     *
     * @param self 參數
     * @param minimumValue 參數
     * @return float
     */
    protected float coerceAtLeast(float self, float minimumValue) {
        if (self < minimumValue) {
            return minimumValue;
        } else {
            return self;
        }
    }

    /**
     * coerceAtMost
     *
     * @param self 參數
     * @param maximumValue 參數
     * @return float
     */
    protected float coerceAtMost(float self, float maximumValue) {
        if (self > maximumValue) {
            return maximumValue;
        } else {
            return self;
        }
    }
}
